package com.umakr.ax.common.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Maps;
import com.umakr.ax.common.mapper.SpreadMapper;
import com.umakr.ax.common.model.RadioStation;
import com.umakr.ax.common.model.Spread;
import com.umakr.ax.common.model.SpreadContent;
import com.umakr.ax.common.model.SpreadRes;
import com.umakr.ax.common.vo.SpreadActivityVO;
import com.umakr.ax.core.base.BaseService;
import com.umakr.ax.utils.FormatPhotoUrlUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 电台推广 服务实现类
 * </p>
 *
 * @author Umakr-Code-Generator
 * @since 2017-12-30
 */
@Service
public class SpreadService extends BaseService<SpreadMapper, Spread>{
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    @Autowired
    SpreadContentService contentService;

    @Autowired
    SpreadResService resService;

    @Autowired
    RadioStationService stationService;

    @Autowired
    Environment env;

    public Object findSpreadUrlBySpId(Long spId ){
        String couponStaticHtmlHost = env.getProperty("coupon.static.html.host");
        if(spId != null){
            Spread spread = queryById(spId);
            if(spread != null){
                Map<String ,Object> map = Maps.newHashMap();
                map.put("url",couponStaticHtmlHost+spread.getMobileUrl());
                map.put("title",spread.getSpreadTitle());
                return resultMap(map,1,"");
            }else{
                return resultMap(0,"未找到相关的活动");
            }
        }else{
            return resultMap(0,"活动id不能为空");
        }
    }





    public Object delSpread(Long id, Integer spreadStatus) {
        int errorCode = 0 ;
        String msg = "";
        Long currentTime = System.currentTimeMillis();
        Spread sp = queryById(id);
        if(sp != null){
            if(sp.getSpreadExpireEnd() == null || sp.getSpreadExpireStart() == null){
                sp.setDelStatus(spreadStatus);
                update(sp);
                errorCode = 1 ;
                msg = "删除成功";
            }else {
                if (sp.getSpreadExpireEnd().getTime() <= currentTime || sp.getSpreadExpireStart().getTime() >= currentTime) {
                    sp.setDelStatus(spreadStatus);
                    update(sp);
                    errorCode = 1;
                    msg = "删除成功";
                } else {
                    msg = "删除失败，活动正在进行中，不能删除";
                }
            }
        }else{
            msg = "删除失败，该活动已被删除";
        }

        return resultMap(errorCode,msg);
    }



    public Object saveOrUpdateSpread(Spread spread,String spreadUrl,Long resultContentId){

        Date cDate = new Date();
        spread.setCreateTime(cDate);
        RadioStation rs = stationService.queryById(spread.getRsId());
        if(spread.getSpreadType() == 1){
            //直播
            spread.setSpreadStatus(1);
            spread.setResCode(rs.getRsCode());
            spread = update(spread);
        }else if(spread.getSpreadType() == 0){
            //广告
            spread.setSpreadStatus(0);
            spread = update(spread);

            EntityWrapper<SpreadRes> ssEn = new EntityWrapper<>();
            ssEn.eq("rs_id",spread.getRsId());
            ssEn.eq("spread_id",spread.getId());
            SpreadRes res = resService.selectOne(ssEn);
            if(res == null) {
                res = new SpreadRes();
            }
            res.setSpreadId(spread.getId());
            res.setRsId(spread.getRsId());
            res.setResCode(spread.getResCode());
            res.setResPath(spreadUrl);
            resService.update(res);

            EntityWrapper<SpreadContent> scEn = new EntityWrapper<>();
            scEn.eq("spread_id",spread.getId());
            SpreadContent content = contentService.selectOne(scEn);
            if(content != null) {
                content.setSpreadId(null);
                contentService.update(content);
            }
        }

        if(resultContentId != null &&resultContentId != -1){
            SpreadContent content = contentService.queryById(resultContentId);
            content.setSpreadId(spread.getId());
            spread.setMobileUrl(content.getMobileUrl());
            spread.setSpreadResultPage(content.getActivityTitle());
            contentService.update(content);
            update(spread);
        }

        return resultMap(1,"保存成功");
    }


    public Object findSpreadByResCode(String[] resCodes) {
        String aliUrl = env.getProperty("oss.img.path");
        String couponStaticHtmlHost = env.getProperty("coupon.static.html.host");
        try {
            if(resCodes.length > 0){
                String resCode = "'" + StringUtils.join(resCodes,"','") + "'";
                List<SpreadActivityVO> spreadList = baseMapper.findSpreadByResCode(resCode,simpleDateFormat.format(new Date()));
                for(SpreadActivityVO vo : spreadList){
                    vo.setSellerLogo(FormatPhotoUrlUtil.formatString(aliUrl,vo.getSellerLogo()));
                    vo.setMobileUrl(couponStaticHtmlHost+vo.getMobileUrl());
                }
                return resultMap(spreadList,1,"");
            }else{
                return resultMap(0,"资源编码为空");
            }
        }catch (Exception e){
            return resultMap(0,"");
        }
    }
}
